package com.hermit.qmst.request;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;

import com.hermit.qmst.tools.Common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownLoadAPK extends AsyncTask<String, Integer, String> {

    private String TAG = "DownLoadAPK";
    private String mUrl;
    private RequestTaskInterface mTaskObserver;
    private UpdateProcessInterface mProcessObserver;
    private Context mContext;

    public DownLoadAPK(RequestTaskInterface pRequestTaskInterface, String pUrl, UpdateProcessInterface pUpdateProcessInterface, Context pContext) {
        mUrl = pUrl;
        mTaskObserver = pRequestTaskInterface;
        mProcessObserver = pUpdateProcessInterface;
        mContext = pContext;
    }


    protected String doInBackground(String... params) {

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return updateForSD();
        } else {
            return updateForChache();
        }

    }

    private String updateForSD() {
        String filePath = Environment.getExternalStorageDirectory() + "/" + Common.mApkName;
        URL url;
        try {
            url = new URL(mUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(60 * 1000);
            con.setRequestMethod("GET");
            if (con.getResponseCode() == con.HTTP_OK) {
                int lenght = con.getContentLength();
                InputStream in = con.getInputStream();
                File fileOut = new File(filePath);
                FileOutputStream out = new FileOutputStream(fileOut);
                byte[] bytes = new byte[1024];
                int c;
                int count = 0;

                while ((c = in.read(bytes)) != -1) {
                    out.write(bytes, 0, c);
                    count = count + c;
                    int progress = (int) (((float) count / lenght) * 100);
                    publishProgress(progress);
                }
                in.close();
                out.close();
            }


        } catch (Exception e) {
            e.printStackTrace();

        } finally {

        }


        return filePath;
    }


    private String updateForChache() {
        String filePath = mContext.getDir("aaa", Context.MODE_PRIVATE | Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE).getPath() + "/" + Common.mApkName;

        URL url = null;
        try {
            url = new URL(mUrl);

            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setConnectTimeout(60 * 1000);
            con.setRequestMethod("GET");
            if (con.getResponseCode() == con.HTTP_OK) {
                int lenght = con.getContentLength();

                InputStream in = con.getInputStream();
                File fileOut = new File(filePath);
                FileOutputStream out = new FileOutputStream(fileOut);
                byte[] bytes = new byte[1024];
                int c;
                int count = 0;

                while ((c = in.read(bytes)) != -1) {
                    out.write(bytes, 0, c);
                    count = count + c;
                    int progress = (int) (((float) count / lenght) * 100);
                    publishProgress(progress);
                }
                in.close();
                out.close();
            }


        } catch (Exception e) {
            e.printStackTrace();

        } finally {

        }


        return filePath;
    }

    @Override
    protected void onPostExecute(String result) {
        //super.onPostExecute(result);
        mTaskObserver.postExecute(result);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

        //super.onProgressUpdate(values);
        mProcessObserver.processUpate(values[0]);
    }


}
